package com.sqsf.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component("myAspect")
@Aspect
public class MyAspect {
    @Pointcut("execution(* com.sqsf.aop.Target.select*(..))")
    public void select(){};

    @Pointcut("execution(* com.sqsf.aop.Target.add(..))")
    public void add(){};

    @Before("MyAspect.select()")
    public void before(){
        System.out.println("前置增强");
    }

    @After("MyAspect.select()")
    public void after(){
        System.out.println("后置增强");
    }

    @Around("MyAspect.add()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        Object[] args = joinPoint.getArgs();
        System.out.println("环绕通知"+ Arrays.toString(args));
        Object proceed = joinPoint.proceed(args);
        System.out.println("环绕通知"+proceed);
        return proceed;
    }

    @AfterThrowing(value = "MyAspect.add()",throwing = "e")
    public void afterThrowing(Exception e){
        System.out.println("异常通知"+e);
    }

    @AfterReturning(value = "MyAspect.add()",returning = "result")
    public void afterReturning(Object result){
        System.out.println("返回通知"+result);
    }
}
